package com.homer.android.activity.configure;

import java.util.List;

import org.apache.log4j.Logger;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;

import com.homer.android.R;
import com.homer.android.sdk.action.device.IEspDeviceNew;
import com.homer.android.sdk.devcie.IEspDevice;
import com.homer.android.sdk.util.BSSIDUtil;
import com.homer.android.sdk.util.EspStrings;
import com.homer.iot.esptouch.base.api.EspBaseApiUtil;

public class DeviceConfigureProgressDialog extends DeviceConfigureDialogAbs implements OnDismissListener,
OnCancelListener
{
private final Logger log = Logger.getLogger(DeviceConfigureProgressDialog.class);

protected ApInfo mApInfo;

protected ProgressDialog mDialog;

private LocalBroadcastManager mBroadcastManager;

public DeviceConfigureProgressDialog(DeviceConfigureActivity activity, IEspDeviceNew device, ApInfo apInfo)
{
    super(activity, device);
    
    mApInfo = apInfo;
    mBroadcastManager = LocalBroadcastManager.getInstance(mActivity);
}

public void show()
{
    stopAutoRefresh();
    
    IntentFilter filter = new IntentFilter(EspStrings.Action.DEVICES_ARRIVE_STATEMACHINE);
    mBroadcastManager.registerReceiver(mReceiver, filter);
    mDialog = new ProgressDialog(mActivity);
    mDialog.setMessage(mActivity.getString(R.string.esp_configure_configuring));
    mDialog.setCanceledOnTouchOutside(false);
    mDialog.setOnDismissListener(this);
    mDialog.setOnCancelListener(this);
    mDialog.show();
    
    checkHelpState();
    
    mUser.saveApInfoInDB(mApInfo.bssid, mApInfo.ssid, mApInfo.password, mDevice.getBssid());
    mUser.doActionConfigure(mDevice, mApInfo.ssid, mApInfo.type, mApInfo.password);
}

@Override
public void onDismiss(DialogInterface dialog)
{
    Log.i("DeviceConfigureProgressDialog",
        "@@@@@@@@@@@@@@@@@@@@@@@@@@@@onDismiss@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    
    mBroadcastManager.unregisterReceiver(mReceiver);
    
    resetAutoRefresh();
}

@Override
public void onCancel(DialogInterface dialog)
{
    Log.i("DeviceConfigureProgressDialog",
        "@@@@@@@@@@@@@@@@@@@@@@@@@@@@onCancel@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    
    mDevice.cancel(true);
}

private BroadcastReceiver mReceiver = new BroadcastReceiver()
{
    
    @Override
    public void onReceive(Context context, Intent intent)
    {
        log.error("onReceive = " + intent.getAction());
        log.error("Configure bc before " + mDevice);
        mUser.doActionDevicesUpdated(true);
        log.error("Configure bc after " + mDevice);
        
        if (mDevice.getDeviceState().isStateActivating())
        {
            log.debug("mDevice.getDeviceState().isStateActivating()");
            
            checkHelpActivating();
            
            // Configure success
            mDialog.setMessage(mActivity.getString(R.string.esp_configure_result_success));
            mDialog.dismiss();
            mActivity.setResult(Activity.RESULT_OK);
            mActivity.finish();
            
            connectAP();
            log.debug("mDevice.getDeviceState().isStateActivating()");
        }
        else if (mDevice.getDeviceState().isStateDeleted())
        {
            // Configure failed
            mDialog.setMessage(mActivity.getString(R.string.esp_configure_result_failed));
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(true);
            
            checkHelpDeleted();
        }
    }
    
    private void connectAP()
    {
        List<IEspDevice> userDeviceList = mUser.getDeviceList();
        for (IEspDevice device: userDeviceList)
        {
            if (BSSIDUtil.isEqualIgnore2chars(device.getBssid(), mApInfo.bssid))
            {
                // connect last ap
                String lastSSID = mUser.getLastConnectedSsid();
                if (!TextUtils.isEmpty(lastSSID))
                {
                    EspBaseApiUtil.enableConnected(lastSSID);
                }
                return;
            }
        }
        // Connect the configured wifi
        EspBaseApiUtil.enableConnected(mApInfo.ssid, mApInfo.type, mApInfo.password);
    }
};


protected void checkHelpState()
{
}

protected void checkHelpActivating()
{
}

protected void checkHelpDeleted()
{
}
}

